package com.example.springboot.controller;

import com.example.springboot.common.Result;
import com.example.springboot.entity.BookReservation;
import com.example.springboot.service.impl.BookReservationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import java.util.List;

@CrossOrigin
@RequestMapping("/reservation")
@RestController
@Slf4j
@Api(tags = "预约接口")
public class BookReservationController {

    @Autowired
    private BookReservationService bookReservationService;


    @PostConstruct
    public void init() {
        System.out.println("✅ BookReservationController 已加载到 Spring 容器中");
    }

    @GetMapping("/test")
    public String test() {
        return "Controller 正常工作！";
    }

    /**
     * 根据id修改预约信息
     * @param reservation
     * @return
     */
    @ApiOperation("修改预约信息")
    @PutMapping("/update")
    public Result update(@RequestBody BookReservation reservation){
        log.info("修改分类信息：{}",reservation);

        bookReservationService.updateReservation(reservation);

        return Result.success("修改预约信息成功");
    }
/**
 * 预约查询（根据用户信息）
 */
@ApiOperation("根据用户id查询预约记录")
@GetMapping("/user/{userId}")
public List<BookReservation> list(@PathVariable Long userId){
    log.info("用户id：{}",userId);
    return bookReservationService.getReservationsByUserId(userId);
}

    /**
     * 根据图书id查询预约记录
     * @param bookId
     * @return
     */
    @ApiOperation("根据图书id查询预约记录")
    @GetMapping("/book/{bookId}")
    public List<BookReservation> getReservationsByBookId(@PathVariable Long bookId) {
        log.info("图书id：{}",bookId);
        return bookReservationService.getReservationsByBookId(bookId);

    }

    /**
     *根据用户id和状态查询预约记录
     * @param userId
     * @param status
     * @return
     */
    @ApiOperation("根据用户id和状态查询预约记录")
    @GetMapping("/user/{userId}/status/{status}")
    public List<BookReservation> getReservationsByUserIdAndStatus(@PathVariable Long userId, @PathVariable String status) {
        log.info("用户id：{}，状态：{}",userId,status);
        return bookReservationService.getReservationsByUserIdAndStatus(userId, status);
    }

    /**
     * 跟据预约状态查询预约信息
     * @param status
     * @return
     */
    @ApiOperation("跟据预约状态查询预约信息")
    @GetMapping("/status/{status}")
    public List<BookReservation> getReservationsByStatus(@PathVariable String status) {
        log.info("状态：{}",status);
        return bookReservationService.getReservationsByStatus(status);
    }
    /**
     * 新增预约
     * @param bookReservation
     * @return
     */
    @ApiOperation("新增预约")
    @PostMapping("/save")
    public Result save(@RequestBody BookReservation bookReservation) {
        log.info("新增预约：{}",bookReservation);
        bookReservationService.save(bookReservation);
        return Result.success();
    }
}
